package com.zg.autoflow.web;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.autoflow.req.AutoflowInherentFieldsAddInfoReq;
import com.zg.autoflow.req.AutoflowInherentFieldsIdsReq;
import com.zg.autoflow.req.AutoflowInherentFieldsPageReq;
import com.zg.autoflow.service.AutoflowInherentFieldsService;
import com.zg.common.core.R;
import com.zg.common.core.dao.BaseEntity;
import com.zg.common.core.dao.Inherent.AutoflowInherentFields;
import com.zg.common.core.dao.Inherent.dao.AutoflowInherentFieldsDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* @author zg
*/
@RestController
@RequiredArgsConstructor
@Api(tags = {"固有字段配置"})
public class AutoflowInherentFieldsController {
	private final AutoflowInherentFieldsDao autoflowInherentFieldsDao;
	private final AutoflowInherentFieldsService autoflowInherentFieldsService;

	@PostMapping("/autoflow/inherent/fields/add")
	@ApiOperation("新增或修改固有字段配置")
	public R addOrUpdate(@RequestBody @Valid AutoflowInherentFieldsAddInfoReq req) {
		 autoflowInherentFieldsService.addOrUpdate(req);
		return R.success();
	}

	@PostMapping("/autoflow/inherent/fields/del")
	@ApiOperation("删除固有字段配置")
	public R del(@RequestBody@Valid AutoflowInherentFieldsIdsReq req) {
		autoflowInherentFieldsService.del(req.getInherentFieldsId());
		return R.success();
	}

	@PostMapping("/autoflow/inherent/fields/page")
	@ApiOperation("分页查询固有字段配置")
	public R<IPage<AutoflowInherentFields>> page(@RequestBody@Valid AutoflowInherentFieldsPageReq pageReq) {
		LambdaQueryWrapper<AutoflowInherentFields> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(AutoflowInherentFields::getInherentId,pageReq.getInherentId());
		String searchStr = pageReq.getSearchStr();
		if (StrUtil.isNotBlank(searchStr)) {
			lambdaQueryWrapper.like(AutoflowInherentFields::getSearchStr, "%" + searchStr + "%");
		}
		lambdaQueryWrapper.orderByDesc(BaseEntity::getCreateTime);
		IPage<AutoflowInherentFields> pb = autoflowInherentFieldsDao.page(
			new Page<>(pageReq.getPage(), pageReq.getPageSize()),
			lambdaQueryWrapper);
		return R.success(pb);
	}
}
